//我的收藏！！！！！！！！！！！！！！！！！！！！！！！
var isover=0;
var username;//用户中文名
var currentIndex=1;//页码
$().ready(function(){
    $.ajax({
        type: "post",
        datatype: "json",
        url: userurl,
        data:{"userId":id},
        success:function(data){
            username=data.Data.nick;
            aj();
        }
    });
});
//ba4d39d74ed0fdcb
var ajaxrunning="no";
function aj(){
    if(isover===0){
        ajaxrunning="yes";
        $.ajax({
            type:"get",
            datatype: "json",
            url: mycollurl+"?userId="+id+"&pageSize=15&currentIndex="+currentIndex,
            success:function(data){
                //console.log(data);
                if(data.Message==="没有数据"){
                    isover=1;//没有数据了
                    $("#waiting").css("display","none");
                    $("#nonews").css("display","block");
                }
                //有数据
                else{
                    if(data.Data&&data.Data.length>0){
                        if(data.Data.length<15){
                            $("#waiting").css("display","none");
                            $("#nonews").css("display","block");
                        }
                        var num=0;
                        for(var i=0;i<data.Data.length;i++){
                            num++;
                            if(num===data.Data.length){
                                ajaxrunning="no";
                            }
                            var unick1="";
                            if(data.Data[i].isHideName==1&&data.Data[i].userId!=id){
                                unick1="匿名";
                            }else{
                                unick1="<a href='otherpeoplepage.html?otherid="+data.Data[i].userId+"'>"+data.Data[i].userNick+"</a>";
                            }
                            var reg=/([0-9/\-]*)/;
                            var tim=data.Data[i].cTime.match(reg)[0];
                            if(data.Data[i].cTime){
                                tim=getDateDiff(getDateTimeStamp(data.Data[i].cTime));
                            };
                            if(data.Data[i].type==1){
                                $("#questionlist").append("<div class='pagedlist_item cid"+data.Data[i].id+"'><div class='feed_item feed_type_question'><div class='EventHeader'><span class='senduser'>"+"新闻"+"&nbsp;&nbsp;"+tim+"</span> <span class='job'></span></div><h2><a class='question_link' href='"+data.Data[i].url+"'>"+data.Data[i].title+"</a></h2><div class='ActionBar Question'><a class='WriteAnswer Button gofinish' id='cid"+data.Data[i].id+"'>取消收藏</a></div></div></div>")
                            }
                            if(data.Data[i].type==3){
                                $("#questionlist").append("<div class='pagedlist_item cid"+data.Data[i].id+"'><div class='feed_item feed_type_question'><div class='EventHeader'><span class='senduser'>"+"经验"+"</span> <span class='job'>&nbsp;&nbsp;"+unick1+"&nbsp;&nbsp;"+tim+"</span></div><h2><a class='question_link' href='shareexppage.html?eid="+data.Data[i].contentId+"'>"+data.Data[i].title+"</a></h2><div class='ActionBar Question'><a class='WriteAnswer Button gofinish' id='cid"+data.Data[i].id+"'>取消收藏</a></div></div></div>")
                            }
                            if(data.Data[i].type==2){
                                $("#questionlist").append("<div class='pagedlist_item cid"+data.Data[i].id+"'><div class='feed_item feed_type_question'><div class='EventHeader'><span class='senduser'>"+"问题"+"</span> <span class='job'>  &nbsp;&nbsp;"+unick1+"&nbsp;&nbsp;"+tim+"</span></div><h2><a class='question_link' href='questiondetail.html?qid="+data.Data[i].contentId+"'>"+data.Data[i].title+"</a></h2><div class='ActionBar Question'><a class='WriteAnswer Button gofinish' id='cid"+data.Data[i].id+"'>取消收藏</a></div></div></div>")
                            }
                        }
                        //取消收藏功能部分
                        var cancelcoll=document.getElementsByClassName("gofinish");
                        for(var r1=0;r1<cancelcoll.length;r1++){
                            cancelcoll[r1].index=r1;
                            var cid="";
                            var reg=/([0-9]+)/;
                            cancelcoll[r1].onclick=function(){
                                cid=cancelcoll[this.index].id.match(reg)[0];
                                var classname=cancelcoll[this.index].id;
                                console.log(cid);
                                document.getElementById("bg").style.display="block";
                                document.getElementById("bgm").style.display="block";
                                document.getElementById("nobtn").onclick=function(){
                                    document.getElementById("bg").style.display="none";
                                    document.getElementById("bgm").style.display="none";
                                };
                                document.getElementById("okbtn").onclick=function(){
                                    $.ajax({
                                        type:"post",
                                        datatype:"json",
                                        url:deletecollectionurl,
                                        data:{"collectionId":cid},
                                        success:function(data){
                                            document.getElementById("bg").style.display="none";
                                            document.getElementById("bgm").style.display="none";
                                            console.log(data);
                                            $("."+classname).remove();
                                        }
                                    })
                                }
                            }
                        }
                    }
                    else{
                        //$("#nonews").css("display","block");
                        $("#waiting").css("display","none");
                        $("#nonews").css("display","block");
                    }

                }

            }
        })
    }

}
$(window).scroll(function(){
    var mainheight=$(".home_feed").height();
    //console.log("加载区域高度："+mainheight);
    var windowheight=$(window).height();
    //console.log("窗口高度："+windowheight);
    var what=$(".home_feed").offset().top;
    var chajuli=$(".home_feed").offset().top - $(window).scrollTop();
    var cz=windowheight-mainheight+50;
    //console.log("窗口与加载差值："+cz);
    //console.log("差值"+chajuli);
    if(chajuli<cz&&ajaxrunning==="no"){
        currentIndex++;
        aj();
    }
});
//搜索
document.onkeydown=function(event){
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if(e && e.keyCode==13){ // enter 键
        var word=document.getElementById("searchinput").value;
        if(word){
            if(word.match(/^\s+$/g)){
                console.log("没有填信息")
            }else{
                location.href="search.html?searchKey="+word;
            }
        }
    }
};
document.getElementById("gotosearch").onclick=function(){
    var word=document.getElementById("searchinput").value;
    if(word){
        if(word.match(/^\s+$/g)){
            console.log("没有填信息")
        }else{
            location.href="search.html?searchKey="+word;
        }
    }
};